package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.INewsTypeDao;
import com.cn.wanxi.model.NewsTypeModel;
import com.cn.wanxi.util.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsTypeDaoImpl implements INewsTypeDao {
    @Override
    public NewsTypeModel getNewsType(int id) {
        String sql = "select * from news_type where id="+id;
        NewsTypeModel newsType = null;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        try {
            while (rs.next()){
                newsType = new NewsTypeModel();
                newsType.setId(rs.getInt("id"));
                newsType.setName(rs.getString("name"));
                newsType.setHref(rs.getString("href"));
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return newsType;
    }

    @Override
    public int addNewsType(NewsTypeModel newsType) {
        String sql = "insert into news_type (name,href,create_time) values('"+ newsType.getName() +
                "','"+ newsType.getHref() +"','"+ newsType.getCreateTime() +"')";
        return JDBCUtils.excute(sql);
    }

    @Override
    public int editNewsType(NewsTypeModel newsType) {
        String sql = "update news_type set name='"+ newsType.getName() +
                "',href='"+ newsType.getHref() +"',update_time='"+ newsType.getUpdateTime() +
                "' where id="+newsType.getId();
        return JDBCUtils.excute(sql);
    }

    @Override
    public int deleteNewsType(int id) {
        String sql = "delete from news_type where id="+id;
        return JDBCUtils.excute(sql);
    }

    @Override
    public List<NewsTypeModel> getNewsTypeList() {
        String sql = "select * from news_type";
        List<NewsTypeModel> list = new ArrayList<>();
        NewsTypeModel newsType = null;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        try {
            while (rs.next()){
                newsType = new NewsTypeModel();
                newsType.setId(rs.getInt("id"));
                newsType.setName(rs.getString("name"));
                newsType.setHref(rs.getString("href"));
                newsType.setCreateTime(rs.getString("create_time"));
                newsType.setUpdateTime(rs.getString("update_time"));
                newsType.setEnable(rs.getInt("enable"));
                newsType.setNote(rs.getString("note"));
                list.add(newsType);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<NewsTypeModel> getNewsTypeList(int page, int size) {
        String sql = "select * from news_type limit "+ (page-1)*size +","+size;
        List<NewsTypeModel> list = new ArrayList<>();
        NewsTypeModel newsType = null;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        try {
            while (rs.next()){
                newsType = new NewsTypeModel();
                newsType.setId(rs.getInt("id"));
                newsType.setName(rs.getString("name"));
                newsType.setHref(rs.getString("href"));
                newsType.setCreateTime(rs.getString("create_time"));
                newsType.setUpdateTime(rs.getString("update_time"));
                newsType.setEnable(rs.getInt("enable"));
                newsType.setNote(rs.getString("note"));
                list.add(newsType);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<NewsTypeModel> getNewsTypeList(int page, int size, String sort) {
        String sql = "select * from news_type order by "+ sort +" limit "+ (page-1)*size +","+size;
        List<NewsTypeModel> list = new ArrayList<>();
        NewsTypeModel newsType = null;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        try {
            while (rs.next()){
                newsType = new NewsTypeModel();
                newsType.setId(rs.getInt("id"));
                newsType.setName(rs.getString("name"));
                newsType.setHref(rs.getString("href"));
                newsType.setCreateTime(rs.getString("create_time"));
                newsType.setUpdateTime(rs.getString("update_time"));
                newsType.setEnable(rs.getInt("enable"));
                newsType.setNote(rs.getString("note"));
                list.add(newsType);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<NewsTypeModel> searchNewsType(String sql) {
        List<NewsTypeModel> list = new ArrayList<>();
        NewsTypeModel newsType = null;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        try {
            while (rs.next()){
                newsType = new NewsTypeModel();
                newsType.setId(rs.getInt("id"));
                newsType.setName(rs.getString("name"));
                newsType.setHref(rs.getString("href"));
                newsType.setCreateTime(rs.getString("create_time"));
                newsType.setUpdateTime(rs.getString("update_time"));
                newsType.setEnable(rs.getInt("enable"));
                newsType.setNote(rs.getString("note"));
                list.add(newsType);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public long getNewsTypeCount() {
        String sql = "select count(*) from news_type";
        return JDBCUtils.getCount(sql);
    }
}
